#include "zbTime.h"

void initTime(void)
{
	// generate int every 1ms for timing stuff
	if(SysTick_Config(SystemCoreClock / 100))
	{
		// error during configure of systick
		mygVars->status |= ERR_SYSTICK; // set error bit
	}
}


/**
  * @brief  
  *        
  *         
  * @note   T
  *			
  *			
  * @param  none
  * @retval none
  */
u32 getMSTick()
{
	return mygVars->milliseconds;
}
/**
  * @brief  
  *        
  *         
  * @note   T
  *			
  *			
  * @param  none
  * @retval none
  */
u32 getSecondTick()
{
	return mygVars->seconds;
}

void setSecondTick(u32 newSec)
{
	mygVars->seconds = newSec;
}


/**
  * @brief 
  *       
  *         
  * @note   
  * @param  none
  * @retval none
  */
void systickSetup(void)
{
	if (SysTick_Config(SystemCoreClock / 1000))
	{ 
	/* Capture error */ 
		while (1);
	}
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{ 
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  { 
    TimingDelay--;
  }
}

/**
  * @brief int on sysclk, every ms
  *       
  *         
  * @note   
  * @param  none
  * @retval none
  */
void SysTick_Handler(void)
{
	TimingDelay_Decrement();
	mygVars->milliseconds++;

	// every 100 milliseconds incrament the seconds
	if(mygVars->milliseconds % 100 == 0)
	{
		mygVars->seconds++;
	}

	// if we are getting the fq on pc9?
	//if(CCP2Ticks != 0)
	//{
	//	CCP2FQ = ((float)CCP2Ticks)*8*1000; 	// one tick per 8 pulses, *10 because the interval is on ms which are 100 per sec
	//	CCP2Ticks = 0;						// reset to 0
	//}
}
